import { request } from 'umi';

export async function getFolwRunInfo(daoName: string, runID: number) {
  return request<API.RunInfo>(`/api/restricted/workflow/RunInfo/${daoName}/${runID}`);
}

export async function batchSubmit(
  daoName: string,
  runIDs: number[],
  userID: number,
  desc: string,
  bType: string,
) {
  const data = new FormData();
  data.append('runIDs', JSON.stringify(runIDs));
  data.append('userID', '' + userID);
  data.append('desc', desc);
  data.append('bType', bType);
  return request<string>(`/api/restricted/workflow/BatchSubmit/${daoName}`, {
    method: 'POST',
    body: data,
  });
}

export async function submit(
  runID: number,
  userID: number,
  utid: number,
  desc: string,
  bType: string,
) {
  const data = new FormData();
  data.append('userID', '' + userID);
  data.append('utid', '' + utid);
  data.append('desc', desc);
  data.append('bType', bType);
  return request<'OK' | undefined>(`/api/restricted/workflow/Submit/${runID}`, {
    method: 'POST',
    body: data,
  });
}

export async function sendBack(runIDs: number[], userID: number, backOrg: number, desc: string) {
  const data = new FormData();
  data.append('runIDs', JSON.stringify(runIDs));
  data.append('userID', '' + userID);
  data.append('backOrg', '' + backOrg);
  data.append('desc', desc);
  return request<'OK' | undefined>(`/api/restricted/workflow/SendBack`, {
    method: 'POST',
    body: data,
  });
}

export async function recall(runIDs: number[], userID: number, recallStepID: number, desc: string) {
  const data = new FormData();
  data.append('runIDs', JSON.stringify(runIDs));
  data.append('userID', '' + userID);
  data.append('recallStepID', '' + recallStepID);
  data.append('desc', desc);
  return request<'OK' | undefined>(`/api/restricted/workflow/Recall`, {
    method: 'POST',
    body: data,
  });
}

export async function abort(runIDs: number[], daoName: string) {
  const data = new FormData();
  data.append('runIDs', JSON.stringify(runIDs));
  return request<'OK' | undefined>(`/api/restricted/workflow/Abort/${daoName}`, {
    method: 'POST',
    body: data,
  });
}

export async function activation(runIDs: number[]) {
  const data = new FormData();
  data.append('runIDs', JSON.stringify(runIDs));
  return request<'OK' | undefined>(`/api/restricted/workflow/Activation`, {
    method: 'POST',
    body: data,
  });
}

export async function deleteFolw(runID: number) {
  return request<'OK' | undefined>(`/api/restricted/workflow/Delete/${runID}`, {
    method: 'POST',
  });
}

export async function isManager(permID: number) {
  return request<boolean>(`/api/restricted/workflow/IsManager/${permID}`, {
    method: 'POST',
  });
}

export async function removeKeys(daoName: string, keys: number[]) {
  const data = new FormData();
  data.append('PrimaryKeyValues', JSON.stringify(keys));
  return request<'OK' | undefined>(`/api/restricted/WorkFlowDeletes/${daoName}`, {
    method: 'POST',
    body: data,
  });
}
